package com.server.llh.llhserverwork.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;


import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.adapter.LlhMyGridViewAdapter;

import java.util.List;

/**
 * Created by ZTF on 2017-03-30.
 * 自定义弹出二级服务项菜单
 */

public class MyEpListPopuView extends PopupWindow implements PopupWindow.OnDismissListener {
    private GvItemClickCallBack gvItemClickCallBack=null;
    private ExpandableListView expandableListView=null;
    private   List<List<String>> itemList;
    private Context context;
    private View rootView;
    private LlhMyEplvAdapter llhMyexAdapter=null;
    /**
     * 每个分组的名字的集合
     */
    private final List<String> groupList;

    public MyEpListPopuView(Activity context, final List<List<String>> itemList,
                            final List<String> groupList, GvItemClickCallBack gvItemClickCallBack){
        super();
        this.context=context;
        this.itemList=itemList;
        this.groupList=groupList;
        this.gvItemClickCallBack=gvItemClickCallBack;
        initPopuWindow( context,itemList,
                 groupList);
    }

    private void initPopuWindow(Activity context, List<List<String>> itemList,
                                List<String> groupList) {
        rootView = LayoutInflater.from(context).inflate(
                R.layout.llh_ex_popuwindow, null);
        int hight=context.getWindowManager().getDefaultDisplay().getHeight();
        int width=context.getWindowManager().getDefaultDisplay().getWidth();

        expandableListView = (ExpandableListView) rootView.findViewById(R.id.expandableList);
        this.setContentView(rootView);
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOnDismissListener(this);
        this.setBackgroundDrawable(new BitmapDrawable());
        // this.setAnimationStyle(animStyle);
        this.setContentView(rootView);
        this.llhMyexAdapter =  new LlhMyEplvAdapter(context,
                groupList, itemList);
        this.expandableListView.setAdapter(llhMyexAdapter);

        // 隐藏分组指示器
        this. expandableListView.setGroupIndicator(null);
        // 默认展开第一组
        this. expandableListView.expandGroup(0);
        this. expandableListView
                .setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

                    @Override
                    public void onGroupExpand(int groupPosition) {
//                        for (int i = 0, count = itemList
//                                .getExpandableListAdapter().getGroupCount(); i < count; i++) {
//                            if (groupPosition != i) {// 关闭其他分组
//                                itemList.collapseGroup(i);
//                            }
//                        }
                    }
                });
    }
    /**
     * ExpandableListView 适配器
     */
    public class LlhMyEplvAdapter extends BaseExpandableListAdapter {

        private Context mContext;

        /**
         * 每个分组的名字的集合
         */
        private List<String> groupList;

        /**
         * 所有分组的所有子项的 GridView 数据集合
         */
        private List<List<String>> itemList;

        private GridView gridView;

        public LlhMyEplvAdapter(Context context, List<String> groupList,
                                List<List<String>> itemList) {
            mContext = context;
            this.groupList = groupList;
            this.itemList = itemList;
        }

        @Override
        public int getGroupCount() {
            return groupList.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return 1;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groupList.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return itemList.get(groupPosition).get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup
                parent) {
            if (null == convertView) {
                convertView = View.inflate(mContext, R.layout.llh_ex_group, null);
            }
            ImageView ivGroup = (ImageView) convertView.findViewById(R.id.iv_group);
            TextView tvGroup = (TextView) convertView.findViewById(R.id.tv_group);
            // 如果是展开状态，就显示展开的箭头，否则，显示折叠的箭头
            if (isExpanded) {
                ivGroup.setImageResource(R.drawable.llh_ic_open);
            } else {
                ivGroup.setImageResource(R.drawable.llh_ic_close);
            }
            // 设置分组组名
            tvGroup.setText(groupList.get(groupPosition));
            return convertView;
        }

        @Override
        public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View
                convertView, ViewGroup parent) {
            if (null == convertView) {
                convertView = View.inflate(mContext, R.layout.llh_ex_item, null);
            }
            // 因为 convertView 的布局就是一个 GridView，
            // 所以可以向下转型为 GridView
            gridView = (GridView) convertView;
            // 创建 GridView 适配器
            LlhMyGridViewAdapter gridViewAdapter = new LlhMyGridViewAdapter(mContext, itemList.get
                    (groupPosition));
            gridView.setAdapter(gridViewAdapter);
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(mContext, "点击了第" + (groupPosition + 1) + "组，第" +
                            (position + 1) + "项", Toast.LENGTH_SHORT).show();
                    if (gvItemClickCallBack!=null){
                        String onClick="点击了第" + (groupPosition + 1) + "组，第" +
                                (position + 1) + "项";
                        gvItemClickCallBack.callBack(onClick);
                    }
                }
            });
            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }
    }

    @Override
    public void onDismiss() {

    }

    public interface GvItemClickCallBack{
            void callBack(String string);
    }
}
